﻿    using System;
    using System.Data;
    using System.IO;
    using UnityEditor;
    using UnityEngine;

#if UNITY_EDITOR
    public abstract class DataParser
    {
        static DataParser()
        {
            JsonHelper.LineEnding = "LF";
        }

        /// <summary>
        /// 单表解析入口
        /// </summary>
        /// <param name="table"></param>
        public virtual void DoParse(DataTable table)
        {
        }

        public virtual void DoParse(DataTable[] table)
        {
        }

        /// <summary>
        /// 单表解析，带ID的
        /// </summary>
        public virtual void DoParse(DataTable table, int id)
        {
        }

        //表名
        public abstract string GetLocalCsvName();


        /// <summary>
        /// 获取[]括号内的信息
        /// </summary>
        protected string FetchValueInSquareBrackets(string str, bool must = true)
        {
            try
            {
                var s = str.IndexOf('[');
                var e = str.IndexOf(']');
                return str.Substring(s + 1, e - s - 1);
            }
            catch
            {
                if (must)
                {
                    Debug.LogError(str);
                    return null;
                }
                else
                {
                    return str;
                }
            }
        }

        private string GetLocalFilePath()
        {
            return GetLocalCsvFilePath(GetLocalCsvName());
        }

        protected static float PercentStrToFloat(string str, int decimals = 2)
        {
            if (str.IndexOf('%') != -1)
            {
                str = str.Replace("%", "");
                str = str.Replace(" ", "");
                var f = float.Parse(str);
                f *= 0.01f;
                f = (float)Math.Round(f, decimals);
                return f;
            }

            return (float)Math.Round(float.Parse(str), decimals);
        }

        protected string GetLocalCsvFilePath(string fileName)
        {
            return Application.dataPath + "/DataConfig/LocalCsv/" + fileName + ".csv";
        }

        private string GetContentFromLocalFile()
        {
            var path = GetLocalFilePath();
            if (!File.Exists(path))
                throw new FileNotFoundException(path);

            return TextFileHelper.ReadTextFromFile(path);
        }

        protected string GetConfigFilePath()
        {
            return Application.dataPath + "/YooRes/ConfigFile/";
        }

        protected string MakeJsonEasyToRead(string json)
        {
            return JsonHelper.FormatJson(json);
        }

        protected void WriteFile(string fileContent, string fileName)
        {
            var filePath = GetConfigFilePath() + fileName;
            if (fileName.StartsWith("/"))
            {
                filePath = Application.dataPath + fileName;
            }

            TextFileHelper.SaveTextToFile(filePath, fileContent);
            AssetDatabase.Refresh();
            Debug.Log("更新文件:" + fileName);
            Debug.Log("文件内容:" + fileContent);
        }

    }
#endif

